<template>
  <div class="data-table-template">
    <div class="table-header">
      <h2 class="table-title">{{ title }}</h2>
      <div class="action-buttons" v-if="showActions">
        <slot name="actions">
          <button @click="$emit('load')" class="btn btn-blue">
            加载示例数据
          </button>
          <button @click="$emit('clear')" class="btn btn-red">清空数据</button>
        </slot>
      </div>
    </div>
    <div class="data-table">
      <table>
        <thead>
          <tr>
            <th
              v-for="header in headers"
              :key="header.key"
              :class="header.class"
            >
              {{ header.label }}
            </th>
          </tr>
        </thead>
        <tbody>
          <slot></slot>
        </tbody>
      </table>
    </div>
  </div>
</template>

<script setup lang="ts">
interface Header {
  key: string;
  label: string;
  class?: string;
}

defineProps<{
  title: string;
  headers: Header[];
  showActions?: boolean;
}>();

defineEmits<{
  (e: "load"): void;
  (e: "clear"): void;
}>();
</script>

<style scoped>
.data-table-template {
  width: 100%;
}

.table-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
}

.table-title {
  font-size: 20px;
  font-weight: 700;
  color: #111827;
}

.action-buttons {
  display: flex;
  gap: 8px;
}

.btn {
  padding: 8px 16px;
  border-radius: 8px;
  font-size: 14px;
  transition: background-color 0.2s;
  cursor: pointer;
  border: none;
}

.btn-blue {
  background-color: #eff6ff;
  color: #2563eb;
}

.btn-blue:hover {
  background-color: #dbeafe;
}

.btn-red {
  background-color: #fef2f2;
  color: #dc2626;
}

.btn-red:hover {
  background-color: #fee2e2;
}

.data-table {
  margin-top: 20px;
  overflow-x: auto;
}

table {
  border-collapse: collapse;
  width: 100%;
  min-width: 600px;
  table-layout: fixed;
}

th,
td {
  border: 1px solid #e5e7eb;
  padding: 4px;
  text-align: center;
  vertical-align: middle;
  height: 50px;
  position: relative;
}

th {
  background-color: #f9fafb;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  padding: 8px;
  font-weight: 500;
  color: #4b5563;
}

/* 输入框容器样式 */
td :deep(.input-field) {
  margin: 0;
  height: 100%;
  display: flex;
  align-items: center;
}

td :deep(.input-field label),
td :deep(.input-field p) {
  display: none;
}

td :deep(.input-field .relative) {
  flex: 1;
  height: 100%;
}

td :deep(.input-field input) {
  width: 100%;
  height: 100%;
  text-align: center;
  border: none;
  padding: 0 24px 0 8px;
  border-radius: 0;
}

td :deep(.input-field input:focus) {
  outline: 2px solid #3b82f6;
  outline-offset: -2px;
}

td :deep(.input-field .relative span) {
  height: 100%;
  display: flex;
  align-items: center;
  background: #f8f9fa;
  padding: 0 8px;
  border-left: 1px solid #e5e7eb;
}

/* 响应式调整 */
@media (max-width: 640px) {
  .table-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 12px;
  }

  .action-buttons {
    width: 100%;
  }

  .btn {
    flex: 1;
    text-align: center;
  }
}
</style>
